package recursion;

/**
 * @description: 迷宫问题小球走路
 * @author: wangjun
 * @create: 2022-05-03
 */
public class Migong {

    public static void main(String[] args) {
//        int[][] map = new int[3][1];
//        System.out.println(movingCount(map, 0, 0, 0));
//        System.out.println(movingCount(2,3,1));
//        print(map);
//        setWay(map, 1, 1);
//        print(map);
//        char[][] board = new char[][]{{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}};
//        char[][] board = new char[][]{{'a', 'b'}};
//        System.out.println(exist(board, "ba"));

        int[] array = {-2,1,-3,4,-1,2,1,-5,4};
//        int[] array = {-2,-1,-3};
        System.out.println(maxSubArray(array));
    }

    public static int maxSubArray(int[] nums) {
        int result = 0;
        int maxValue = nums[0];
        for(int i : nums){
            result = Math.max(result + i,i);
            maxValue = Math.max(result,maxValue);
        }
        return maxValue;
    }

    public static int maxSubArray1(int[] nums) {
        int min = 0;
        int result = nums[0];
        int prefixSum = 0;
        for (int value : nums) {
            prefixSum += value;
            result = Math.max(result, prefixSum - min);
            if (prefixSum < min) {
                min = prefixSum;
            }
        }
        return result;
    }

    public static boolean exist(char[][] board, String word) {
        char[] words = word.toCharArray();
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (stringExists(board, i, j, words, 0)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean stringExists(char[][] board, int i, int j, char[] word, int k) {
        if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != word[k]) {
            return false;
        }
        if (k == word.length - 1) {
            return true;
        }
        board[i][j] = '\0';
        boolean result = stringExists(board, i + 1, j, word, k + 1) ||
                stringExists(board, i, j + 1, word, k + 1) ||
                stringExists(board, i - 1, j, word, k + 1) ||
                stringExists(board, i, j - 1, word, k + 1);
        board[i][j] = word[k];
        return result;
    }

    public static int[][] map(int m, int n) {
        int[][] ints = new int[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                ints[i][j] = 0;
            }
        }

        for (int i = 0; i < n; i++) {
            ints[0][i] = 1;
            ints[m - 1][i] = 1;
        }

        for (int j = 0; j < m; j++) {
            ints[j][0] = 1;
            ints[j][n - 1] = 1;
        }

        ints[3][1] = 1;
        ints[3][2] = 1;
        return ints;
    }

    /**
     * 1，挡板，2，通过，3，已经走过，但是走不通
     *
     * @param [map, m, n]
     * @return boolean
     */
    public static boolean setWay(int[][] map, int i, int j) {
        if (map[6][5] == 2) {
            return true;
        }
        if (map[i][j] == 0) {
            map[i][j] = 2;
            // 向右
            if (setWay(map, i, j + 1)) {
                return true;
            }
            // 向下
            if (setWay(map, i + 1, j)) {
                return true;
            }
            // 向上
            if (setWay(map, i - 1, j)) {
                return true;
            }
            // 向左
            if (setWay(map, i, j - 1)) {
                return true;
            }
            map[i][j] = 3;
        }
        return false;
    }

    public static void print(int[][] map) {
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                System.out.printf("%d\t", map[i][j]);
            }
            System.out.println();
        }
    }

    private static int count = 1;

    public static int movingCount(int[][] map, int i, int j, int k) {
        if (i < 0 || i > map.length) {
            return 0;
        }
        if (j < 0 || j > map[0].length) {
            return 0;
        }
        if (!checkIsArriving(i, j, k) || map[i][j] == 1) {
            return 0;
        }
        map[i][j] = 1;
        // 向右
        return movingCount(map, i, j + 1, k) +
                // 向上
                movingCount(map, i - 1, j, k) +
                // 向左
                movingCount(map, i, j - 1, k) +
                // 向下
                movingCount(map, i + 1, j, k) + 1;
    }

    public static boolean checkIsArriving(int i, int j, int k) {
        int row = i / 10 + i % 10;
        int col = j / 10 + j % 10;
        return (row + col) <= k;
    }


    public static int movingCount(int m, int n, int k) {
        int res = 0;
        int[][] flag = new int[m][n];
        res = helper(m, n, k, 0, 0, flag);
        return res;

    }

    public static int helper(int m, int n, int k, int l, int r, int[][] flag) {
        if (l == m || r == n || l < 0 || r < 0 || flag[l][r] == 1) return 0;
        if (getSum(l, r) > k) return 0;
        flag[l][r] = 1;
        int up = helper(m, n, k, l - 1, r, flag);
        int down = helper(m, n, k, l + 1, r, flag);
        int left = helper(m, n, k, l, r - 1, flag);
        int right = helper(m, n, k, l, r + 1, flag);

        return up + down + left + right + 1;
    }

    public static int getSum(int l, int r) {
        int res = 0;
        res += (l / 100);
        res += (l % 100 / 10);
        res += (l % 10);
        res += (r / 100);
        res += (r % 100 / 10);
        res += (r % 10);
        return res;
    }
}